﻿using GetMan.Core.Common;
using GetMan.Core.DB.Entities;
using GetMan.Core.DB.Services.Collection;
using GetMan.Core.Extensions;
using GetMan.Core.infrastructure.BaseC;
using GetMan.Core.Models;
using Rubyer;
using Rubyer.Models;
using System;
using System.ComponentModel.Composition;

namespace GetMan.ViewModels
{
    /// <summary>
    /// 集合管理
    /// </summary>
    [Export(typeof(NodeViewModel))]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    public class NodeViewModel : ViewModelBase<NodeViewModel>, IDialogDataContext
    {
        [Import]
        private INodeService nodeService;

        public NodeViewModel() : base()
        {

        }

        ~NodeViewModel()
        {

        }

        #region 属性
        /// <summary>
        /// 主键
        /// </summary>
        private long id;
        public long Id
        {
            get => id;
            set
            {
                id = value;
                NotifyOfPropertyChange(() => Id);
            }
        }
        /// <summary>
        /// 工作空间
        /// </summary>
        private long workSpaceId;
        public long WorkSpaceId
        {
            get => workSpaceId;
            set
            {
                workSpaceId = value;
                NotifyOfPropertyChange(() => WorkSpaceId);
            }
        }

        /// <summary>
        /// 父级主键
        /// </summary>
        private long parentId;
        public long ParentId
        {
            get => parentId;
            set
            {
                parentId = value;
                NotifyOfPropertyChange(() => ParentId);
            }
        }

        /// <summary>
        /// 名称
        /// </summary>
        private string _nodeName;
        public string NodeName
        {
            get => _nodeName;
            set
            {
                _nodeName = value;
                NotifyOfPropertyChange(() => NodeName);
            }
        }

        /// <summary>
        /// 描述
        /// </summary>
        private string _description;
        public string Description
        {
            get => _description;
            set
            {
                _description = value;
                NotifyOfPropertyChange(() => Description);
            }
        }

        /// <summary>
        /// 节点类型
        /// </summary>
        public NodeTypeEnum NodeType
        {
            get; set;
        }
        #endregion


        private string title;
        public string Title
        {
            get => title;
            set
            {
                title = value;
                NotifyOfPropertyChange(() => Title);
            }
        }
        public event Action<object> RequestClose;
        public void OnDialogOpened(object parameters)
        {
            var @params = (IParameters)parameters;
            var getRet = @params.TryGetValue<long>(DialogCommanKeys.Id, out var currId);
            var getWorkId = @params.TryGetValue<long>(DialogCommanKeys.WorkSpaceId, out var currWorkId);
            var getNodeType = @params.TryGetValue<NodeTypeEnum>(DialogCommanKeys.NodeType, out var currNodeType);
            var getParentId = @params.TryGetValue<long>(DialogCommanKeys.ParentId, out var currParentId);
            Id = getRet ? currId : 0;
            WorkSpaceId = getWorkId ? currWorkId : 0;
            ParentId = getParentId ? currParentId : 0;
            if (!getNodeType)
                throw new Exception("节点类型未配置");

            NodeType = currNodeType;
            var model = nodeService.Get(Id);
            if (model != null)
            {
                switch (NodeType)
                {
                    case NodeTypeEnum.Collection:
                        Title = "集合编辑";
                        break;
                    case NodeTypeEnum.Folder:
                        Title = "文件夹编辑";
                        break;
                    case NodeTypeEnum.Request:
                        Title = "请求编辑";
                        break;
                }
                NodeName = model.NodeName;
                Description = model.Description;
            }
            else
            {
                switch (NodeType)
                {
                    case NodeTypeEnum.Collection:
                        Title = "集合新增";
                        break;
                    case NodeTypeEnum.Folder:
                        Title = "文件夹新增";
                        break;
                    case NodeTypeEnum.Request:
                        Title = "请求新增";
                        break;
                }
            }
        }

        #region 事件
        public void Save()
        {
            if (NodeName.IsNull())
            {
                Message.Error("名称不能为空");
                return;
            }

            //更新
            if (Id > 0)
            {
                var entity = nodeService.Get(Id);
                entity.NodeName = NodeName;
                entity.Description = Description;
                entity.UpdateTime = DateTime.Now;
                entity.ParentId = ParentId;
                int ret = nodeService.Update(entity);
                if (ret > 0)
                {
                    Message.Success("更新成功");
                    Close(true, entity);
                    return;
                }
                Message.Error("更新失败");
            }
            else
            {
                var entity = new NodesEntity();
                entity.NodeName = NodeName;
                entity.NodeType = NodeType;
                entity.WorkSpaceId = WorkSpaceId;
                entity.Description = Description;
                entity.UpdateTime = DateTime.Now;
                entity.CreateTime = DateTime.Now;
                entity.ParentId = ParentId;
                entity.IsEnable = true;
                long retId = nodeService.Add(entity);
                if (retId > 0)
                {
                    Message.Success("新增成功");
                    entity.Id = retId;
                    Close(true, entity);
                    return;
                }
                Message.Error("新增失败");
            }
        }

        public void Close(bool isRefresh, NodesEntity model = null)
        {
            var parameters = new Parameters();
            //是否刷新
            parameters.Add(DialogCommanKeys.Refresh, isRefresh);
            parameters.Add(DialogCommanKeys.Model, model);
            this.RequestClose?.Invoke(parameters);
        }
        public void Close()
        {
            this.Close(false, null);
        }
        #endregion
    }
}